/**
 * 		-	ad-creatif	-
 * 		-	20/07/2012	-
 *
 * This class creates a bunch of buttons using the photonstorm class "FlxButtonPlus"
 * You give the list in params name and function to call while clicking and it'll do the rest
 * 
 * 
 **/

package tools
{
	import com.adobe.protocols.dict.events.ErrorEvent;
	
	import flash.events.Event;
	
	import org.flixel.FlxGroup;
	import org.flixel.FlxSprite;
	import org.flixel.plugin.photonstorm.FlxButtonPlus;
	
	import tools.MyFlxSprite;
	
	public class ButtonFactory extends FlxGroup
	{
		
		/**
		 *	Constructor
		 * 
		 * @param	Params	{label, callback}
		 * @param	X		position X, use null for middle alignement
		 * @param	Y		position Y
		 * @param	Width	height will automaticaly fit width the background image you gave
		 * @param	Height	weight will automaticaly fit width the background image you gave
		 * 
		 * */
		
		
		public function ButtonFactory(params:Array, X:* = null, Y:Number = 0,  template:Object = null )
		{
			super();
			
			for(var i:int; i< params.length; i++){
				var callBack:Function = (params[i]['callBack'] && params[i]['callBack'] is Function) ? params[i]['callBack'] as Function: null;
				//var callBackLabels:Array = params[i]['label'] ? [params[i]['label']] : null;
				var callBackParams:Array = params[i]['param'] ? [params[i]['param']] : [params[i]['label']];
				
				var buttonSize:Array = [];
				
				if(template) {
					if(template.btnImg){
						var buttonShape:FlxSprite = new MyFlxSprite(0,0).loadGraphicWithBitmap(template.btnImg);
						buttonSize = [buttonShape.width,buttonShape.height];
					}
					if(template.btnHoverImg) 
						var buttonHover:FlxSprite = new MyFlxSprite(0,0).loadGraphicWithBitmap(template.btnHoverImg);
					
					if(template.btnSize) buttonSize = template.btnSize;
					
				}
				
				if(buttonSize.length == 0) 
					buttonSize = [150,30];
					
					
				var button:FlxButtonPlus = new FlxButtonPlus(0, Y + i * buttonSize[1] , callBack, callBackParams, params[i]['label'], buttonSize[0], buttonSize[1]);
				if(buttonShape) button.loadGraphic(buttonShape,buttonHover ? buttonHover : buttonShape);
				
				if(template && template.fontSize && template.fontColor){
					button.textNormal.setFormat(null, template.fontSize, template.fontColor, "center", 0xff000000);
					button.textHighlight.setFormat(null, template.fontSize, 0xffffffff, "center", 0xff000000);
				}

				if(params[i]['hoverCallBack'] && params[i]['hoverCallBack'] is Function)
					button.setMouseOverCallback(params[i]['hoverCallBack'] as Function,[params[i]['label']]);
				
				
				button.screenCenter();
			 	if(X) button.x = X;
				
				add(button);
				
			}
			
		}
		
		public function setStyle():void{
		}
	}
}